/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;

namespace FourRoads.Links.Controls
{
	using DataProvider;
	/// <summary>
	/// Summary description for ResLinkBreadCrumb.
	/// </summary>
	public class ResLinkBreadCrumb : Literal 
	{
		protected CSContext csContext = CSContext.Current;
		protected Queue crumbs = new Queue();
		protected HtmlAnchor anchor;

		public ResLinkBreadCrumb()
		{

		}
		
		protected override void CreateChildControls() 
		{
			RecursivlyAddSections(ResLinkSection.GetSection(CSContext.Current.SectionID));
		}

		private void AddQueueItem(string name , int id , string description)
		{
			crumbs.Enqueue( GetAnchor(name, ResLinkUrls.Instance().ViewSection(id) , description) );
		}

		protected void RecursivlyAddSections(ResLinkSection section) 
		{
			//Add the home page in
			if (ShowHome)
			{
				AddQueueItem(ResourceManager.GetString("ResLink_BreadCrumb_RootName") , -1 , ResourceManager.GetString("ResLink_BreadCrumb_RootDescription"));
			}
			
			ResLinkDataProvider dp = ResLinkDataProvider.Instance();

			ArrayList col = dp.GetParentBreadCrumbs(section);
			
			foreach (ResLinkDataProvider.BasicSection parent in col)
			{
				AddQueueItem(parent.Name , parent.SectionID ,  parent.Description);
			}

			AddQueueItem(section.Name , section.SectionID , section.Description);
		}

		protected Control GetAnchor(string innerText, string href ) 
		{
			return GetAnchor(innerText, href ,string.Empty); 
		}

		protected Control GetAnchor(string innerText, string href , string title) 
		{
			anchor = new HtmlAnchor();
			anchor.InnerHtml = innerText;
			anchor.HRef = href;

			if (title != string.Empty)
			{
				anchor.Title = title;
			}

			return anchor;
		}

		protected override void Render(HtmlTextWriter writer) 
		{

			while(crumbs.Count > 0) 
			{
				HtmlAnchor a = (HtmlAnchor) crumbs.Dequeue();

				// add title for hover over
				if (a.Title == string.Empty)
					a.Title = a.InnerHtml;

				if ((crumbs.Count > 0) && (a.InnerHtml.Length > 15))
					a.InnerHtml = a.InnerHtml.Substring(0,15) + "...";

				a.RenderControl(writer);

				if (crumbs.Count > 0)
					writer.Write(ResourceManager.GetString("BreadCrumb_Seperator"));

			}
            
		}
		#region Properties
		
		/// <summary>
		/// Controls whether or not the root element for the home is shown
		/// </summary>
		[
		System.ComponentModel.DefaultValue( false ),
		]
		public virtual Boolean ShowHome 
		{
			get 
			{
				Object state = ViewState["ShowHome"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return false;
			}
			set 
			{
				ViewState["ShowHome"] = value;
			}
		}

		/// <summary>
		///  Determines whether or not links are hook-ed up.
		/// </summary>
		[
		System.ComponentModel.DefaultValue( true ),
		]
		public virtual Boolean EnableLinks 
		{
			get 
			{
				Object state = ViewState["EnableLinks"];
				if ( state != null ) 
				{
					return (Boolean)state;
				}
				return true;
			}
			set 
			{
				ViewState["EnableLinks"] = value;
			}
		}

		#endregion
	}
}
